package com.driving.single.frontend.partner.service.impl;

import com.driving.single.common.domain.TbPartner;
import com.driving.single.common.entity.DrivingResult;
import com.driving.single.common.entity.PageResult;
import com.driving.single.common.partner.service.CommonPartnerCommentService;
import com.driving.single.common.partner.service.CommonPartnerService;
import com.driving.single.common.redis.service.CommonRedisPartnerService;
import com.driving.single.frontend.partner.service.FrontendPartnerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class FrontendPartnerServiceImpl implements FrontendPartnerService {
    @Autowired
    private CommonPartnerService commonPartnerService;
    @Autowired
    private CommonRedisPartnerService commonRedisPartnerService;
    @Autowired
    private CommonPartnerCommentService commonPartnerCommentService;

    /**
     * 实现  找陪练页面查询 接口
     * @param page        当前页
     * @param rows        页显示数
     * @param area        查询条件：区域(学校名称)
     * @param licenseType 查询条件：驾照类型
     * @param minPrice    查询条件：最小价格
     * @param maxPrice    查询条件：最大价格
     */
    @Override
    public DrivingResult selectPartnerListByConditionWithPage(Integer page, Integer rows, String area,
                                                              String licenseType, Integer minPrice, Integer maxPrice) {
        // 1.价格输入校验
        if (maxPrice != null && minPrice != null && maxPrice < minPrice) {
            return DrivingResult.error("价格区间有误，请核实输入！");
        }
        // 2.分页查询数据库
        PageResult pageResult = this.commonPartnerService.selectPartnerListByConditionWithPage(page, rows, area,
                licenseType, minPrice, maxPrice);
        if (pageResult == null || pageResult.getResult().size() == 0) {
            return DrivingResult.error("查无数据");
        }
        return DrivingResult.ok(pageResult);
    }

    /**
     * 实现根据主键查询教练接口
     * @param id 主键
     */
    @Override
    public DrivingResult selectTbPartnerById(Long id) {
        // 1.查询Redis缓存
        try {
            TbPartner redisTbPartner = this.commonRedisPartnerService.selectRedisTbPartnerById(id);
            if (redisTbPartner != null) {
                return DrivingResult.ok(redisTbPartner);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        // 2.查询数据库
        TbPartner tbPartner = this.commonPartnerService.selectTbPartnerById(id);
        if (tbPartner == null) {
            return DrivingResult.error("查无数据");
        }
        // 3.插入Redis缓存
        try {
            this.commonRedisPartnerService.insertRedisTbPartner(tbPartner);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return DrivingResult.ok(tbPartner);
    }

    /**
     * 实现根据陪练ID   分页查询陪练评论   接口
     * @param partner_id 陪练主键
     * @param page     当前页
     * @param rows     页显示数
     */
    @Override
    public DrivingResult selectPartnerCommentByPartnerId(Long partner_id, Integer page, Integer rows) {
        PageResult pageResult = this.commonPartnerCommentService.selectPartnerCommentByPartnerId(partner_id,
                page, rows);
        if (pageResult == null || pageResult.getResult().size() == 0) {
            return DrivingResult.error("查无数据");
        }
        return DrivingResult.ok(pageResult);
    }
}
